package com.study.controller;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

import org.eclipse.jdt.internal.compiler.env.IModule.IService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.study.model.pojo.Inventory;
import com.study.model.pojo.Outbound;
import com.study.model.pojo.Product;
import com.study.model.pojo.Transfer;
import com.study.model.pojo.Warehouse;
import com.study.services.IntypeService;
import com.study.services.ProductService;
import com.study.services.TransferService;
import com.study.services.WarehouseService;

@RestController
@CrossOrigin
public class TransferController {
	@Autowired
	TransferService tService;
	@Autowired
	ProductService pService;
	@Autowired
	WarehouseService wService;
	
	/**
	 * 查询所有调拨信息
	 * @return
	 */
	@RequestMapping("/findtransfer")
	public JSONObject findtransfer() {
		JSONObject json=new JSONObject();
		JSONArray json1 = new JSONArray();
		JSONArray json2 = new JSONArray();
		JSONArray json3 = new JSONArray();
		System.out.println("111111");
		//查询调库信息
		List<Transfer> list = tService.findAll();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		for (Transfer t : list) {
			JSONObject jo=new JSONObject();
			jo.put("traId", t.getTraId());
			jo.put("proId", t.getProduct().getProName());
			jo.put("traOut", t.getTraOut());
			jo.put("traIn", t.getTraIn());
			jo.put("traZdy", t.getTraZdy());
			jo.put("traSdy", t.getTraSdy());
			jo.put("proNum", t.getProNum());
			jo.put("traDate", sdf.format(t.getTraDate()));
			jo.put("traNote", t.getTraNote());
			jo.put("traStatic", t.getTraStatic());
			json1.add(jo);
		}
		//查询仓库信息
		List<Warehouse> list2=wService.findAll();
		for (Warehouse w : list2) {
			JSONObject jo=new JSONObject();
			jo.put("warId", w.getWarId());
			jo.put("warName", w.getWarName());
			jo.put("warKind", w.getWarKind());
			json2.add(jo);
		}
		//查询产品 
		List<Product> list3=pService.findAll();
		for (Product p : list3) {
			JSONObject jo=new JSONObject();
			jo.put("proId", p.getProId());
			jo.put("proName", p.getProName());
			json3.add(jo);
		}
		json.put("warehouse", json2);
		json.put("transfer", json1);
		json.put("product", json3);
		return json;
	}
	
	/**
	 * 新增调库信息
	 * @param transfer 调库信息
	 * @param outKind 调库方式
	 * @param proId 产品名称
	 * @param inbUnit 调库单位
	 * @return
	 */
	@RequestMapping("addtransfer")
	public JSONObject addtransfer(Transfer transfer,String outKind,String proId,String inbUnit) {
		JSONObject json=new JSONObject();
		transfer.setTraId("tra0003");
		transfer.setTraStatic("未审核");
		//通过前台参数查询产品id
		String proid=pService.findByproName(proId);
		//装入map
		HashMap map = new HashMap<>();
		map.put("traId", "tra0004");
		map.put("proId", proid);
		map.put("traOut", transfer.getTraOut());
		map.put("traIn", transfer.getTraIn());
		map.put("traZdy", transfer.getTraZdy());
		map.put("traSdy", transfer.getTraSdy());
		map.put("proNum", transfer.getProNum());
		map.put("traNote", transfer.getTraNote());
		map.put("traStatic", transfer.getTraStatic());
		String returnString=tService.addTransfer(map, proid, transfer, outKind, inbUnit);
		json.put("ans", returnString);
		return json;
	}
	
	/**
	 * 通过审核
	 * @param traId
	 * @return
	 */
	@RequestMapping("/trapass")
	public JSONObject trapass(String traId) {
		JSONObject json=new JSONObject();
		tService.pass(traId);
		json.put("ans", "y");
		return json;
	}
}
